﻿using Sunny.UI;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Threading;
using System.Windows.Forms;

namespace CoreShop.App
{
    [ToolboxItem(true)]
    [DefaultEvent("ValueChanged")]
    public partial class UIKeyboardEditBox : UIDropControl, IToolTip, IHideDropDown
    {
        public UIKeyboardEditBox()
        {
            InitializeComponent();
            edit.KeyDown += Edit_KeyDown;
            edit.Leave += Edit_Leave;
            fullControlSelect = true;
        }

        private void Edit_Leave(object sender, EventArgs e)
        {
            //HideDropDown();
        }

        private void Edit_KeyDown(object sender, KeyEventArgs e)
        {
            /*
            if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up)
            {
                if (!NumPadForm.Visible)
                    ShowDropDown();
            }
            else if (e.KeyCode == Keys.Escape)
            {
                NumPadForm.Close();
            }
            else if (e.KeyCode == Keys.Return)
            {
                if (NumPadForm.Visible)
                {
                    NumPadForm.Close();
                }
            }
            else
            {
                base.OnKeyDown(e);
            }
            */
        }

        private void ShowDropDown()
        {
            KeyboardForm.AutoClose = false;
            keyboardItem.SetDPIScale();
            keyboardItem.SetStyleColor(UIStyles.ActiveStyleColor);

            if (!KeyboardForm.Visible)
            {
                //NumPadForm.Show(this.ParentForm, 0, 768 - 228);
                KeyboardForm.Show(this.ParentForm, 0, this.ParentForm.Height - 310);
            }

            edit.Focus();
        }

        public Control ExToolTipControl()
        {
            return edit;
        }

        public void HideDropDown()
        {
            try
            {
                if (KeyboardForm != null && KeyboardForm.Visible)
                    KeyboardForm.Close();
            }
            catch
            {
            }
        }

        public event EventHandler DoValueChanged;

        private readonly UIKeyboardItem keyboardItem = new UIKeyboardItem();

        public UIDropDown keyboardForm;

        private UIDropDown KeyboardForm
        {
            get
            {
                if (keyboardForm == null)
                {
                    keyboardForm = new UIDropDown(keyboardItem);

                    if (keyboardForm != null)
                    {
                        keyboardForm.VisibleChanged += NumBoardForm_VisibleChanged;
                        keyboardForm.ValueChanged += NumBoardForm_ValueChanged;
                    }
                }

                return keyboardForm;
            }
        }

        public const int WM_CHAR = 256;
        private void NumBoardForm_ValueChanged(object sender, object value)
        {
            int start = edit.SelectionStart;
            switch ((int)value)
            {
                case 1:
                    edit.Clear();
                    break;
                case 95:
                    edit.Text = edit.Text.Left(start) + "-" + edit.Text.Middle(start, edit.Text.Length);
                    start += 1;
                    edit.SelectionStart = start;
                    edit.Select(start, 0);
                    break;
                case 46:
                    edit.Text = edit.Text.Left(start) + "." + edit.Text.Middle(start, edit.Text.Length);
                    start += 1;
                    edit.SelectionStart = start;
                    edit.Select(start, 0);
                    break;
                case 13:
                    OkButtonClick?.Invoke(this, Text);
                    break;
                default:
                    Sunny.UI.Win32.User.PostMessage(edit.Handle, WM_CHAR, (int)value, 0);
                    edit.SelectionStart = start;
                    edit.Select(start, 0);
                    //ValueChanged?.Invoke(this, Text);
                    //this.Focus();
                    break;
            }            
            DoValueChanged?.Invoke(this, null);
        }

        public delegate void OnValueChanged(object sender, string value);
        public event OnValueChanged ValueChanged;

        public delegate void OnOkButtonClick(object sender, string value);
        public event OnOkButtonClick OkButtonClick;

        private void NumBoardForm_VisibleChanged(object sender, System.EventArgs e)
        {
            dropSymbol = SymbolNormal;
            if (KeyboardForm.Visible)
            {
                dropSymbol = SymbolDropDown;
            }

            Invalidate();
        }

        private void UIKeyboardTextBox_ButtonClick(object sender, EventArgs e)
        {
            //if (NumPadForm.Visible)
            //{
            //    NumPadForm.Close();
            //}
            //else
            //{
            //    ShowDropDown();
            //}
        }

        private int symbolSize = 24;
        public int SymbolSize
        {
            get => symbolSize;
            set
            {
                symbolSize = value;
                Invalidate();
            }
        }

        protected override void OnPaintFore(Graphics g, GraphicsPath path)
        {
            g.FillRectangle(GetFillColor(), new Rectangle(Width - Padding.Right, 2, Padding.Right - 1, Height - 4));
            Color color = GetRectColor();
            g.DrawFontImage(dropSymbol, SymbolSize, color, new Rectangle(Width - Padding.Right, 0, Padding.Right, Height), 1, 0);
        }

        private void btnShowKeyboard_Click(object sender, EventArgs e)
        {
            if (KeyboardForm.Visible)
            {
                KeyboardForm.Close();
            }
            else
            {
                ShowDropDown();
            }
        }
    }
}
